#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :st_form.py
# @Time      :2024/10/22 21:38
# @Author    :zhou_yi


import streamlit as st
from utils.functions import datetime, rtn_random_8


# 三方比价-表单输入
def st_three_price_form(key, border=True, btn_label='确定', default_value=None, cb=None):
    if default_value is None:
        default_value = {}

    with st.form(key, border=border):
        c1, c2 = st.columns(2)
        with c1:
            name = st.text_input(label='公司名称', value=default_value.get('name'), key=f"{key}_name")
            address = st.text_input(label='公司地址', value=default_value.get('address'), key=f"{key}_address")
            user = st.text_input(label='联系人', value=default_value.get('user'), key=f"{key}_user")
            tel = st.text_input(label='联系电话', value=default_value.get('tel_2'), key=f"{key}_tel")

        with c2:
            signal_price = st.number_input(label='单价（元/桶/月）',
                                           min_value=0.00,
                                           value=default_value.get('signal_price'),
                                           key=f"{key}_signal_price",
                                           )

            months = st.number_input(label='总月数',
                                     min_value=1,
                                     value=default_value.get('months'),
                                     key=f"{key}_months",
                                     disabled=True,
                                     help="不需要修改"
                                     )

            nums = st.number_input(label='桶数',
                                   min_value=1,
                                   key=f"{key}_nums"
                                   )

            price_cac = signal_price * months * nums
            price = st.number_input(label='总价/优惠',
                                    min_value=0.00,
                                    max_value=price_cac,
                                    value=price_cac,
                                    key=f"{key}_price",
                                    help="可以直接修改,作为优惠报价"
                                    )

        _c1, _c2 = st.columns(2)

        with _c1:  # 签名日期
            sign_date = st.date_input(label='签名日期',
                                      value=default_value.get('sign_date'),
                                      disabled=True,
                                      key=f"{key}_sign_date",
                                      help="起始日期往前10天的日期为起点，前20天内的随机日期"
                                      )

        _dic = {
            'name': name,
            'address': address,
            'user': user,
            'tel': tel,
            'sign_date': sign_date,
            'signal_price': signal_price,
            'months': months,
            'nums': nums,
            'price_cac': price_cac,
            'price': price
        }

        with _c2:
            if cb:
                st.form_submit_button(label=btn_label, on_click=lambda: cb(_dic))
            else:
                st.form_submit_button(label=btn_label)

        return _dic


# 合同-表单输入
def st_contract_form(key, border=False, btn_label='确定', default_value=None, cb=None):
    if default_value is None:
        default_value = {}

    with st.form(key, border=border):
        name = st.text_input(label='公司名称', value=default_value.get('name'), key=f"{key}_name")
        address = st.text_input(label='公司地址', value=default_value.get('address'), key=f"{key}_address")
        tax = st.text_input(label='纳税人识别号', value=default_value.get('tax'), key=f"{key}_tax")
        bank = st.text_input(label='开户行', value=default_value.get('bank'), key=f"{key}_bank")
        account = st.text_input(label='账号', value=default_value.get('account'), key=f"{key}_account")
        tel_1 = st.text_input(label='电话', value=default_value.get('address'), key=f"{key}_tel_1")
        jur_person = st.text_input(label='公司法人', value=default_value.get('jur_person'), key=f"{key}_jur_person")
        user = st.text_input(label='联系人', value=default_value.get('user'), key=f"{key}_user")
        tel_2 = st.text_input(label='联系电话', value=default_value.get('tel_2'), key=f"{key}_tel")

        _dic = {
            "name": name,
            "jur_person": jur_person,
            "address": address,
            "tax": tax,
            "bank": bank,
            "account": account,
            "tel_1": tel_1,
            "relation": jur_person,
            "user": user,
            "tel_2": tel_2
        }

        st.form_submit_button(label=btn_label, on_click=cb)

        return _dic


# 客户信息-表单输入
def st_custom_form(key, btn_label='确认', border=False):
    EM = st.empty()
    with EM:
        with st.form(key=key, border=border, enter_to_submit=False):
            colA = st.columns(2)
            with colA[0]:
                tax = st.text_input('纳税人识别号', key=f"{key}_tax")
                address = st.text_input('注册地址', key=f"{key}_address")
                bank = st.text_input('开户银行', key=f"{key}_bank")
                account = st.text_input('银行账号', key=f"{key}_account")
                tel_1 = st.text_input('电话', key=f"{key}_tel_1")
            with colA[1]:
                ABC = st.text_input('简称', key=f"{key}_ABC")
                relation = st.text_input('项目相关性', key=f"{key}_relation")
                jur_person = st.text_input('法人代表', key=f"{key}_jur_person")
                user = st.text_input('联系人', key=f"{key}_user")
                tel_2 = st.text_input('联系电话', key=f"{key}_tel_2")

            _input = {
                "ABC": ABC,
                "jur_person": jur_person,
                "address": address,
                "tax": tax,
                "bank": bank,
                "account": account,
                "tel_1": tel_1,
                "relation": relation,
                "user": user,
                "tel_2": tel_2
            }

            _sbm = st.form_submit_button(label=btn_label)

            if _sbm:
                EM.empty()

            return _input, _sbm


# 有害垃圾点位-表单输入
def st_waster_point_form(key, border=False, btn_label='确定'):
    EM = st.empty()
    with EM:
        with st.form(key=key, border=border):
            c1, c2 = st.columns(2)
            with c1:
                # _id = st.text_input('编号', key=f'{key}__id')
                # custom = st.text_input('客户名称', key=f'{key}_custom')
                part = st.text_input('管理单位', key=f'{key}_part')
            with c2:
                # point = st.text_input('点位名称', key=f'{key}_point')
                # name = st.text_input('区域名称', key=f'{key}_name')
                code = st.text_input('垃圾桶编号', key=f'{key}_code')

            address = st.text_input('地址', key=f'{key}_address')
            c3, c4 = st.columns(2)
            with c3:
                long = st.text_input('经度', key=f'{key}_long')
                manager = st.text_input('管理方联系人', key=f'{key}_manager')
            with c4:
                lat = st.text_input('纬度', key=f'{key}_lat')
                tel = st.text_input('联系电话', key=f'{key}_tel')
            notice = st.text_input('注意事项', key=f'{key}_notice')

            point_img = st.file_uploader(label='上传图片', accept_multiple_files=True, key=f'{key}_point_img')

            _sbm = st.form_submit_button(btn_label)
            _input = {
                # "_id": _id,
                # "custom": custom,
                # "name": name,
                # "point": point,
                "address": address,
                "long": long,
                "lat": lat,
                "part": part,
                'code': code,
                "manager": manager,
                "tel": str(tel),
                "notice": notice,
                # "point_img": [
                #     f"pic1......{key}",
                #     f"pic2.....{key}"
                # ],
            }
            if _sbm:
                EM.empty()

            return _input, _sbm


# 有害垃圾清运记录-表单输入
def st_clear_waster_record_form(key, border=False, btn_label='确定'):
    with st.form(key=key, border=border, enter_to_submit=False):
        c3, c4 = st.columns(2)
        with c3:
            date = st.date_input('日期', key=f'{key}_date', value=datetime.today())
        with c4:
            time = st.time_input('时间', key=f'{key}_time', value=datetime.now())

        f_pic = st.file_uploader('清运前', key=f'{key}_f_pic')
        b_pic = st.file_uploader('清运后', key=f'{key}_b_pic')

        w_total = st.number_input('合计', min_value=0, key=f'{key}_w_total')
        c1, c2 = st.columns(2)
        w_lst = rtn_random_8(w_total)
        with c1:
            w1 = st.number_input('废电池类', min_value=0, key=f'{key}_w1', value=w_lst[0], disabled=True)
            w2 = st.number_input('废荧光灯管类', min_value=0, key=f'{key}_w2', value=w_lst[1], disabled=True)
            w3 = st.number_input('废药品类', min_value=0, key=f'{key}_w3', value=w_lst[2], disabled=True)
            w4 = st.number_input('过期化妆品', min_value=0, key=f'{key}_w4', value=w_lst[3], disabled=True)
        with c2:
            w5 = st.number_input('废杀虫剂、消毒剂', min_value=0, key=f'{key}_w5', value=w_lst[4], disabled=True)
            w6 = st.number_input('废胶片及废相纸', min_value=0, key=f'{key}_w6', value=w_lst[5], disabled=True)
            w7 = st.number_input('溶剂及其包装物', min_value=0, key=f'{key}_w7', value=w_lst[6], disabled=True)
            w8 = st.number_input('废油漆、硒鼓墨盒', min_value=0, key=f'{key}_w8', value=w_lst[7], disabled=True)

        _sbm = st.form_submit_button(btn_label)

        _input = {
            # 'custom': custom,
            # 'point_name': point,
            "datetime": datetime.combine(date, time),
            "f_pic": '',
            "b_pic": '',
            "w_total": w_total,
            "w1": w1,
            "w2": w2,
            "w3": w3,
            "w4": w4,
            "w5": w5,
            "w6": w6,
            "w7": w7,
            "w8": w8
        }
        if _sbm:
            if not _input.get('w_total'):
                st.warning('填写总数')
                st.stop()

        return _input, _sbm
